<template>
  <el-row class="home" :gutter="20">
    <el-col :span="8">
        <!-- 用户卡片 -->
        <el-card  shadow="hover">
            <div class="user">
                <img :src="getImgUrl(`user`)" alt="">
                <div class="userInfo">
                    <p class="user-info-admin" style="font-size: 40px;font-weight: 500;">Admin</p>
                    <p class="user-info-p" style="color: #999;margin-top: 10px;font-weight: 500;">超级管理员</p>
                </div>
            </div>
            <div class="login-info">
                <p>上次登录的时间：<span>2025-07-04</span></p>
                <p>上次登陆的地点：<span>梅州</span></p>
            </div>
        </el-card>
        <!-- 表格卡片 -->
        <el-card shadow="hover" class="table">
            <el-table :data="tableData">
                <el-table-column
                    v-for="(val,key) in tableLabel"
                    :key="key"
                    :prop="key"
                    :label="val"
                >
                </el-table-column>
            </el-table>
        </el-card>
    </el-col>
    <el-col :span="16">
        <div class="num">
            <!-- 统计卡片 -->
            <el-card
                v-for="item in countData"
                :key="item.name"
                :body-style="{display:'flex',padding:0}">
                <component :is="item.icon" class="icons" :style="{background:item.color}"></component>
                <div class="detail">
                    <p class="price">￥{{ item.value }}</p>
                    <p class="txt">{{ item.name }}</p>
                </div>
            </el-card>
        </div>
        <!-- 线型图卡片 -->
        <el-card class="top-echart" style="height: 260px;">
            <div ref="echart" style="height: 260px;"></div>
        </el-card>
        <div class="graph">
            <!-- 条形图卡片 -->
            <el-card>
                <div ref="userData" style="height: 230px;"> </div>
            </el-card>
            <!-- 饼形图卡片 -->
            <el-card>
                <div ref="videoData" style="height: 230px;"></div>
            </el-card>
        </div>
    </el-col>
  </el-row>
</template>

<script setup>
import { ref, reactive, toRefs, onMounted, getCurrentInstance} from 'vue'
import * as echarts from 'echarts'

// 获取当前组件实例
const { proxy } = getCurrentInstance()

// 定义表单数据和标签
const tableData = ref([])
const tableLabel = ref({
    name: "课程",
    todayBuy: "今日购买",
    monthBuy: "本月购买",
    totalBuy: "总购买",
})

//  定义右上方的统计数据
const countData = ref([])

 // 动态获取用户头像
const getImgUrl = (user)=>{
    return new URL(`../assets/images/${user}.png`,import.meta.url).href
}

// 获取表单数据
const getTableData = async ()=>{
    const data = await proxy.$api.getTableData()
    tableData.value = data.tableData
}

// 获取右上方的统计数据
const getCountData = async ()=>{
    const data = await proxy.$api.getCountData()
    countData.value = data
}

// 定义图表数据
// const chartData = ref([])
// 获取图表数据
const getChartData = async ()=>{
    const {orderData,userData,videoData} = await proxy.$api.getChartData()
    // 线性图表进行x轴以及对应的系列进行赋值
    xOptions.xAxis.data = orderData.date
    xOptions.series = Object.keys(orderData.data[0]).map(val=>({
        name:val,
        data:orderData.data.map(item => item[val]),
        type:'line'
    }))
    // 注册图表
    const oneEchart = echarts.init(proxy.$refs['echart'])
    oneEchart.setOption(xOptions)

    // 对柱状图进行数据渲染
    // x轴标签
    xOptions.xAxis.data = userData.map(item=>item.date)
    // 数据
    xOptions.series = [
        {
        name:'新增用户',
        data:userData.map(item=>item.new),
        type:'bar'
        },
        {
        name:'活跃用户',
        data:userData.map(item=>item.active),
        type:'bar'
        },
    ]
    const twoEchart = echarts.init(proxy.$refs['userData'])
    twoEchart.setOption(xOptions)

    // 对饼形图进行数据渲染
    pieOptions.series = [
        {
            data:videoData,
            type:'pie'
        }
    ]
    const threeEchart = echarts.init(proxy.$refs['videoData'])
    threeEchart.setOption(pieOptions)

    const observer = ref(null)
    //ResizeObserver 如果监视的容器大小变化，如果改变会执行传递的回调
    observer.value = new ResizeObserver(entries => {
        oneEchart.resize()
        twoEchart.resize()
        threeEchart.resize()
    })
    //如果这个容器存在
    if (proxy.$refs["echart"]) {
    //则调用监视器的observe方法，监视这个容器的大小
    observer.value.observe(proxy.$refs["echart"]);
    }
}


//这个是折线图和柱状图 两个图表共用的公共配置
const xOptions = reactive({
      // 图例文字颜色
      textStyle: {
        color: "#333",
      },
      legend: {},
      grid: {
        left: "20%",
      },
      // 提示框
      tooltip: {
        trigger: "axis",
      },
      xAxis: {
        type: "category", // 类目轴
        data: [],
        axisLine: {
          lineStyle: {
            color: "#17b3a3",
          },
        },
        axisLabel: {
          interval: 0,
          color: "#333",
        },
      },
      yAxis: [
        {
          type: "value",
          axisLine: {
            lineStyle: {
              color: "#17b3a3",
            },
          },
        },
      ],
      color: ["#2ec7c9", "#b6a2de", "#5ab1ef", "#ffb980", "#d87a80", "#8d98b3"],
      series: [],
})

// 饼形图
const pieOptions = reactive({
    tooltip: {
        trigger: "item",
    },
    // legend: {},
    color: [
        "#0f78f4",
        "#dd536b",
        "#9462e5",
        "#a6a6a6",
        "#e1bb22",
        "#39c362",
        "#3ed1cf",
    ],
    series: []
})

onMounted (()=>{
    getTableData(),
    getCountData(),
    getChartData()
})
// axios({
//     url:"/api/home/getTableData",
//     method:"GET"
// }).then(res=>{
//     // console.log(res)
//     // 使用mockjs进行拦截axiios请求，把我们制造的数据，根据接口文档
//     if(res.data.code === 200){
//         console.log(res.data.data.tableData)
//         tableData.value = res.data.data.tableData
//     }
// })

</script>

<style scoped lang="less">
.home {
    height: 100%;
    overflow: hidden;
    .user {
        display: flex;
        align-items: center;
        border-bottom: 1px solid #ccc;
        margin-bottom: 20px;
        img {
            width: 120px;
            height: 120px;
            border-radius: 50%;
            margin-right: 30px;
            margin-bottom: 15px;
        }
        .userinfo {
            p {
                line-height: 30px;

            }
        }

    }
    .login-info {
        p{
            line-height: 30px;
            font-size: 14px;
            color: #999;
            span {
                color: #666;
                margin-left: 60px;
            }
        }
    }
    .table {
        margin-top: 8px;
        height: 100%;
    }
    .num {
        display: flex;
        flex-wrap: wrap;
        justify-content: space-between;
        .el-card {
            width: 32%;
            margin-bottom: 10px;
            .icons{
                width: 60px;
                height: 60px;
                font-size: 30px;
                align-items: center;
                line-height: 60px;
                color: #fff;
                float: left;
            }
            .detail {
                margin-left: 15px;
                display: flex;
                flex-direction: column;
                justify-content: center;
                float: left;
                margin-top: 5px;
                .price {
                    font-size: 25px;
                    margin-bottom: 5px;
                }
                .txt {
                    font-size: 14px;
                    text-align: center;
                    color: #ccc;
                }

            }
        }
    }
    .graph {
        margin-top: 10px;
        display: flex;
        justify-content: space-between;
        .el-card {
            width: 48%;
        }
    }
}


</style>